package org.ponly.uno4j;

import org.ponly.common.util.StringUtils;
import org.ponly.uno4j.core.UnoClient;
import org.ponly.uno4j.core.impl.EmbeddedUnoClientImpl;
import org.ponly.uno4j.util.D2s;

import java.io.File;
import java.util.concurrent.Future;

/**
 */
public class Main {

    public static void main(String[] args) throws Exception {
        System.out.println(StringUtils.hasLength("1"));
//        System.out.println(oncePerRequestFilter);
//        System.out.println(StringUtils.hasLength("123"));
        System.exit(0);
        args = new String[]{"D:/a.xlsx"};
//        args = new String[]{"/Users/vacoor/Documents/a.pdf"};
        if (1 > args.length) {
            System.err.println("Main source [target]");
            return;
        }

        String source = args[0];
        String target = 1 < args.length ? args[1] : source + ".swf";

        final File src = new File(source);
        final File dst = new File(target);

        // 1. 创建 OOo 服务进程并启动 (也可手动启动外部程序)
        /*
        String unoUrl = "uno:socket,host=localhost,port=8888;";
        OOoProcess proc = new OOoProcess(unoUrl).start();

        // 2. 创建 OOo 客户端
        // 2.1 创建简单客户端, 并执行任务(串行执行)
        /*
        UnoClient unoClient = new UnoClientImpl(unoUrl);
        Future<?> future = unoClient.submit(new DocToPdfTask(src, new FileOutputStream(dst)));

        // 处理完毕, 关闭客户端
        try {
            future.get();
        } catch (Exception ex) {
            proc.stop();
        } finally {
            unoClient.shutdown();
        }
        */

        // 2.2 创建负载客户端, 并执行任务
        // String[] urls = new String[]{ "uno:pipe,name=pipe-1", "uno:pipe,name=pipe-2", "uno:pipe,name=pipe-3" };
        /*
        UnoClient unoClient = new LbUnoClientImpl(new String[] { unoUrl });
        Future<?> future = unoClient.submit(new DocToPdfTask(src, new FileOutputStream(dst)));

        // 处理完毕, 关闭客户端
        try {
            future.get();
        } catch (Exception ex) {
            proc.stop();
        } finally {
            unoClient.shutdown();
        }

        // 测试完毕, 关闭服务进程
        proc.stop();
        */

        // 3. 使用嵌入式客户端自动创建服务进程
        String connectType = "pipe";
        int startPort = 8888;
        UnoClient unoClient = new EmbeddedUnoClientImpl(connectType, startPort, 1).start();
        Future<?> future; // = unoClient.submit(new DocToPdfTask(src, new FileOutputStream(dst)));
        // unoClient.shutdown();


        // 4. 配合其他工具使用
        D2s d2s = new D2s(unoClient, 2);
        future = d2s.submit(src, new File(src.getAbsolutePath() + ".swf"));

        try {
            future.get();
        } finally {
            d2s.shutdown();
            unoClient.shutdown();
        }
    }
}
